<template>
  <div>
    <Add ref="add" @ok="$refs.page.search()"></Add>
    <Update ref="update" @ok="$refs.page.search()"></Update>
    <ZgAttach
      ref="attach"
      :showBtn="false"
      tableName="test"
      :attTypeArr="attTypeArr"></ZgAttach>
    <ZgNewPage
      ref="page"
      title="表单管理"
      addBtnName="新增表单"
      addBtnIcon="plus-circle"
      :conditions="conditions"
      :batchBtns="batchBtns"
      :dataBtns="dataBtns"
      @search="onSearch"
      @condition="onSearch"
      @add="$refs.add.open()"
      @admin="toAdmin">
      <template slot="columns">
        <a-table-column title="表单名称">
          <template slot-scope="record">
            {{ record.name }}
          </template>
        </a-table-column>
        <a-table-column title="表单编码">
          <template slot-scope="record">
            {{ record.code }}
          </template>
        </a-table-column>
        <!--
        <a-table-column title="字段数量">
          <template slot-scope="record">
            {{ record.columnCount }}
          </template>
        </a-table-column>
        <a-table-column title="视图数量">
          <template slot-scope="record">
            {{ record.viewCount }}
          </template>
        </a-table-column>
        <a-table-column title="公式数量">
          <template slot-scope="record">
            {{ record.formulaCount }}
          </template>
        </a-table-column>
        <a-table-column title="模板数量">
          <template slot-scope="record">
            {{ record.tmplCount }}
          </template>
        </a-table-column>
        <a-table-column title="数据量">
          <template slot-scope="record">
            {{ record.dataCount }}
          </template>
        </a-table-column>
        -->
        <a-table-column title="附件管理">
          <template slot-scope="record">
            {{ record.hasAttach }}
          </template>
        </a-table-column>
        <a-table-column title="校验接口地址">
          <template slot-scope="record">
            {{ record.validUrl }}
          </template>
        </a-table-column>
      </template>
    </ZgNewPage>
  </div>
</template>
<script>
import Add from './Add'
import Update from './Update'
export default {
  components: {
    Add, Update
  },
  data () {
    return {
      conditions: [
        { code: 'name', compType: '单行文本', label: '表单名称', defaultVal: '' },
        { code: 'code', compType: '单行文本', label: '表单编码', defaultVal: '' }
      ],
      batchBtns: [],
      dataBtns: [
        { key: 1, name: '删除', type: 'link', icon: 'fa-trash', isShow: () => { return true } },
        { key: 2, name: '修改', type: 'link', icon: 'fa-pencil', isShow: () => { return true } },
        { key: 3, name: '附件', type: 'link', icon: 'fa-file', isShow: (t) => { return t.hasAttach } }
      ],
      attTypeArr: []
    }
  },
  mounted () {
  },
  methods: {
    onSearch (param) {
      this.$http.get('/zg-autoform/form/page', param, data => {
        this.$refs.page.endSearch(
          {
            total: parseInt(data.total),
            list: data.records.map(t => {
              t.key = t.id
              return t
            })
          }
        )
      })
    },
    toAdmin (param) {
      if (param.key === 1) {
        this.confirm(
        () => {
          this.$http.post('/zg-autoform/form/del', [param.obj.id], () => {
            this.$refs.page.search()
          })
        },
        '确定要删除这条数据么')
      } else if (param.key === 2) {
        this.$refs.update.open(param.obj)
      } else if (param.key === 3) {
        this.attTypeArr = JSON.parse(param.obj.attType)
        this.$refs.attach.open(param.obj)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
